﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class IdentifierDeclarationNode : NodesCollection, ITreeNode
    {
        private LexemeType lexType = LexemeType.ID_DEC;
        private IdentifierType Type;
        private String Name;        

        public IdentifierDeclarationNode(IdentifierType idType, String Identifier)
        {
            Type = idType;
            Name = Identifier;
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Identifier Declared: " + Type.ToString() + " " + Name);
        }
        public List<Command> evalNode(ref Direction next,
                                        ref Dictionary<String, Pair<IdentifierType, object>> Environment)
        {
            List<Command> idDec = new List<Command>();
            if (next != Direction.JUMP)
            {
                
                Command result;

                if (!Environment.ContainsKey(Name))
                {
                    Environment.Add(Name, new Pair<IdentifierType, object>(Type, null));
                    result = new Command(CommandType.ENVIRONMENTMODIFICATION, null, null);
                }
                else
                {
                    result = new Command(CommandType.ERRORALREADYDECLARED, Name, null);
                }

                idDec.Add(result);
                return idDec;
            }
            else
            {
                idDec.Add(new Command(CommandType.VOID, null, null));
                return idDec;
            }
        }
    }
}
